<template>
	<view class="col363636 f26r pl20r pr20r">
		<view class="disFlex aliItCenter h100r borB1SE8e8e8" v-if="optionStatus == 1">
			<view class="w160r teAliRight borR1SE8e8e8">
				充值金额：
			</view>
			<view class="flex1 pl20r">
				<input type="digit" placeholder="请输入充值金额"
					class="f26r h60r" v-model="balance"
				>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8" v-if="optionStatus == 1">
			<view class="w160r teAliRight borR1SE8e8e8">
				赠送金额：
			</view>
			<view class="flex1 pl20r">
				<input type="digit" placeholder="请输入赠送金额"
					class="f26r h60r" v-model="giftAmount"
				>
			</view>
		</view>
		<view class="disFlex pt20r pb20r borB1SE8e8e8" v-if="optionStatus == 2">
			<view class="w160r lh60r teAliRight borR1SE8e8e8">
				充值介绍：
			</view>
			<view class="flex1 pl20r">
				<textarea v-model="introduce" placeholder="请输入充值介绍" auto-height
					class="f26r wPer100 lh60r"
				></textarea>
			</view>
		</view>
		<view class="disFlex aliItCenter h100r borB1SE8e8e8" v-if="optionType == 2">
			<view class="w160r teAliRight borR1SE8e8e8">
				启用/禁用：
			</view>
			<view class="flex1 pl20r">
				<radio-group @change="radioChange1">
					<label v-for="(item, index) in radioAry1" :key="item.value"
						class="wPer50 disInBlo teAliCenter"
					>
						<radio :value="item.value"
							:checked="item.value === radioVal"
							color="#64aeb4" style="transform: scale(0.8)"
						 />
						{{item.radioText}}
					</label>
				</radio-group>
			</view>
		</view>
		<button class="colFff lh70r mt40r f26r backCol64aeb4"
			@click="cliSubBtn"
		>
			提交
		</button>
	</view>
</template>
<script>
import { addRechargeInfo, addRechargeIntroduce, saveRechargeInfo } from '@/utils/api.js'
import { inquiryToLogin } from '@/utils/utils.js'
export default {
	data() {
		return {
			token: '',
			optionType: '',
			optionStatus: '',
			optionItem: {},
			balance: '',
			reality_balance: '', giftAmount: '',
			introduce: '',
			radioAry1: [
				{value: '1', radioText: '启用'},
				{value: '2', radioText: '禁用'}
			],
			radioVal: ''
		}
	},
	onLoad(options) {
		this.optionType = options.type;
		this.optionStatus = options.status;
		if(options.type == 1) {
			if(options.status == 1) {
				uni.setNavigationBarTitle({ title: '新增充值金额' });
			}else if(options.status == 2) {
				uni.setNavigationBarTitle({ title: '新增充值介绍' });
			} 
		}else if(options.type == 2) {
			this.optionItem = JSON.parse(options.item);
			this.radioVal = this.optionItem.type;
			if(options.status == 1) {
				uni.setNavigationBarTitle({ title: '修改充值金额' });
				this.balance = this.optionItem.balance;
				this.giftAmount = Math.round((Number(this.optionItem.reality_balance) - Number(this.balance)) * 100) / 100;
			}else if(options.status == 2) {
				uni.setNavigationBarTitle({ title: '修改充值介绍' });
				this.introduce = this.optionItem.introduce;
			} 
			console.log(11, this.optionItem)
		} 
	},
	onShow() {
		inquiryToLogin().then(res => {
			this.token = res;
		});
	},
	methods: {
		radioChange1(evt) {
			for (let i = 0; i < this.radioAry1.length; i++) {
				if (this.radioAry1[i].value === evt.detail.value) {
					this.radioVal = this.radioAry1[i].value;
					break;
				}
			}
		},
		cliSubBtn() {
			if(this.optionStatus == 1) {
				if(!this.balance) return uni.showToast({ title: '请输入充值金额', icon: 'none' })
				if(!this.giftAmount) return uni.showToast({ title: '请输入赠送金额', icon: 'none' })
			}else if(this.optionStatus == 2) {
				if(!this.introduce) return uni.showToast({ title: '请输入充值介绍', icon: 'none' })
			}
			if(this.optionType == 1) {
				if(this.optionStatus == 1) {
					console.log(this.token)
					addRechargeInfo({
						token: this.token, balance: this.balance,
						reality_balance: Math.round((Number(this.giftAmount) + Number(this.balance)) * 100) / 100
					}).then(res => {
						console.log(res)
						uni.showToast({ title: '新增充值金额成功', duration: 3000 })
						this.balance = '';
						this.giftAmount = '';
					})
				}else if(this.optionStatus == 2) {
					addRechargeIntroduce({
						token: this.token, introduce: this.introduce
					}).then(res => {
						console.log(res)
						uni.showToast({ title: '新增充值介绍成功', duration: 3000 })
						this.introduce = '';
					})
				}
			}else if(this.optionType == 2) {
				if(this.optionStatus == 1) {
					saveRechargeInfo({
						token: this.token, cid: this.optionItem.id, recharge_type: 6,
						balance: this.balance,
						reality_balance: (Math.round((Number(this.giftAmount) + Number(this.balance)) * 100) / 100),
						type: this.radioVal
					}).then(res => {
						uni.showToast({ title: '修改充值金额成功', duration: 3000 })
					})
				}else if(this.optionStatus == 2) {
					saveRechargeInfo({
						token: this.token, cid: this.optionItem.id, recharge_type: 7,
						introduce: this.introduce,
						type: this.radioVal
					}).then(res => {
						console.log(res)
						uni.showToast({ title: '修改充值介绍成功', duration: 3000 })
						this.introduce = '';
					})
				}
			} 
		}
	}
}
</script>
<style>
</style>